<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <template slot="header">
        <el-row type="flex" justify="space-between">
          <div>共{{ total }}条记录</div>
          <el-button round type="primary" size="small" @click="addArticle"
            >+添加面经</el-button
          >
        </el-row>
      </template>
      <!-- 下面是表格
       -->

      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="stem" label="标题" width="180">
        </el-table-column>
        <el-table-column prop="creator" label="作者" width="180">
        </el-table-column>
        <el-table-column prop="likeCount" label="点赞"> </el-table-column>
        <el-table-column prop="views" label="浏览数"> </el-table-column>
        <el-table-column prop="createdAt" label="更新时间"> </el-table-column>
        <el-table-column label="操作" width="100">
          <template v-slot="{ row }">
            <el-row type="flex" justify="space-between">
              <i class="el-icon-view" @click="preview(row.id)"></i>
              <i class="el-icon-edit-outline" @click="adit(row.id)"></i>
              <i class="el-icon-delete" @click="del(row.id)"></i>
            </el-row>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-row type="flex" justify="center">
        <el-pagination background layout="prev, pager, next" :total="1000">
        </el-pagination>
      </el-row>
    </el-card>
    <!-- 抽屉 -->
    <el-drawer title="添加面经" :visible.sync="show">
      <!-- label：设置表单旁边的文字
      label-width：设置 label的宽度
       -->
      <!--
        添加校验规则
        1、给form组件添加model属性
        2、给form组件添加rules属性
        3、给form-item组件添加prop属性
        4、表单要进行双向绑定
        -->
      <el-form ref="form" label-width="80px" :model="article" :rules="rules">
        <el-form-item label="标签" prop="stem">
          <el-input v-model="article.stem"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <!-- 富文本 -->
          <quill-editor
            v-model="article.content"
            @blur="$refs.form.validateField('content')"
          >
          </quill-editor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">确定</el-button>
          <el-button @click="close">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
    <!-- 预览面经抽屉组件 -->
    <el-drawer title="我是标题" :visible.sync="previewShow">
      <div style="padding: 10px 20px">
        <h2>{{ article.stem }}</h2>
        <div class="content" v-html="article.content"></div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
// 引入
import {
  articleList,
  addArticle,
  deleteArticle,
  getArticleDetail,
  updateArticle,
} from "@/api/article";
// 富文本
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";

import { quillEditor } from "vue-quill-editor";

export default {
  components: {
    quillEditor,
  },
  data() {
    return {
      tableData: [],
      show: false,
      total: 0,
      // 当前显示哪一页
      current: 1,
      // 每页多少条数据
      pageSize: 10,
      article: {
        stem: "",
        content: "",
      },
      rules: {
        stem: [{ required: true, message: "请输入标题", trigger: "blur" }],
        content: [{ required: true, message: "请输入内容", trigger: "blur" }],
      },
      previewShow: false,
    };
  },
  created() {
    // 获取面经列表
    this.getArticleList();
  },
  methods: {
    async getArticleList() {
      const res = await articleList({
        // 当前显示哪一页
        current: this.current,
        // 每页多少条数据
        pageSize: this.pageSize,
      });
      console.log(res);
      //   保存表格的数据
      this.tableData = res.data.data.rows;
      // 保存面经的总数量
      this.total = res.data.data.pageTotal;
    },

    // 添加面经
    addArticle() {
      this.show = true;
    },
    // 确定新增
    submit() {
      // 添加校验
      this.$refs.form.validate(async (bool) => {
        // 判断一下
        if (bool) {
          // 校验通过 就调佣接口
          if (this.article.id) {
            await updateArticle({
              id: this.article.id,
              stem: this.article.stem,
              content: this.article.content,
            });
          } else {
            await addArticle(this.article);
          }
          // 刷新数据
          this.getArticleList();
          // 关闭抽屉---封装一下
          this.close();
        }
      });
    },
    // 关闭抽屉组件
    close() {
      this.show = false;
      // 清空数据
      this.article = {
        stem: "",
        content: "",
      };
      // 清空校验的规则
      this.$refs.form.resetFields();
    },
    // 删除面经
    del(id) {
      // 弹出询问框
      this.$confirm("您确定要删除这篇面经吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          // 调用接口
          await deleteArticle({ id });
          // 刷新数据
          this.getArticleList();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 编辑面经
    async adit(id) {
      // 获取框
      this.show = true;
      // 调用接口
      const res = await getArticleDetail({ id });
      // 实现数据的回显
      this.article = res.data.data;
    },
    // 预览面经
    async preview(id) {
      this.previewShow = true;
      // 调用接口
      const res = await getArticleDetail({ id });
      // 实现数据回显
      this.article = res.data.data;
    },
  },
};
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 20px;
}
</style>